package com.jeff.tianti.controller;

import com.jeff.tianti.cms.entity.Company;
import com.jeff.tianti.cms.service.CompanyService;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/company")
@CrossOrigin(maxAge = 3600)
public class CompnayController {
    @Autowired
    private CompanyService companyService;


    /**
     * @Ceator:liuYI
     * @Describe:获取人才天地名企推荐公司列表（根据更新时间进行排序）
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public AjaxResult companyList(@RequestBody PageBean pageBean) {
        Pageable pageable = new PageRequest(pageBean.getCurrentPage(), pageBean.getPageSize(), Sort.Direction.DESC, "updateDate");
        Page<Company> page = companyService.findAllNoDel(pageable);
        return AjaxResult.setSuccessPageDate(page);
    }

    /**
     * @Ceator:liuYI
     * @Describe:人才天地用户添加公司信息
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveCompany(@RequestBody Company company) {
        String id = company.getId();
        if (StringUtils.isNotBlank(id)) {//修改
            companyService.update(company);
        } else {//添加
            Boolean bool = companyService.findByUid(company.getUid());
            if (bool) {
                throw new RuntimeException("用户重复添加公司信息");
            } else {
                if (StringUtils.isBlank(company.getUid())) {
                    throw new RuntimeException("uid:参数缺失");
                }
                company.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
                companyService.save(company);
            }
        }
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:判断用户是否录入公司信息
     */
    @RequestMapping(value = "/isCompany", method = RequestMethod.GET)
    public AjaxResult isCompany() {
        // TODO: 2019\7\27 0027 uid写死
        String uid = "402881c26b7949c3016b794e0b8d0001";
        Boolean bool = companyService.findByUid(uid);
        if (bool) {
            return AjaxResult.setSuccessData(null);
        } else {
            throw new RuntimeException("用户没有录入公司信息系");
        }
    }
}
